package com.ch.dsms_backend.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.ch.dsms_backend.entity.Goods;
import com.ch.dsms_backend.service.GoodsService;

@RestController
@RequestMapping("/goods")
public class GoodsController{
    @Autowired
    private GoodsService goodsService;

    @CrossOrigin //跨域访问
    @RequestMapping("/selectGoodsByGnameAndGid")
    public List<Goods> selectGoodsByGnameAndGid(HttpSession session,String gname,Integer gid){
//        System.out.println("showAllGoods-session:"+session.toString());
        return goodsService.selectGoodsByGnameAndGid(gname,gid);
    }



    @CrossOrigin //跨域访问
    @RequestMapping("/showAllGoods")
    public List<Goods> showAllGoods(HttpSession session){
        return goodsService.showAllGoods();
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectAGood")
    public Goods selectAGood(Integer id){
        return goodsService.selectAGood(id);
    }


    @CrossOrigin //跨域访问
    @RequestMapping("/selectRecommendGoods")
    public List<Goods> selectRecommendGoods(HttpSession session){
        return goodsService.selectRecommendGoods();
    }


    @CrossOrigin //跨域访问
    @RequestMapping("/toAddGoods")
    public String toAddGoods(@ModelAttribute("goods") Goods goods,Model model){
        goods.setIsAdvertisement(0);
        goods.setIsRecommend(1);
        return goodsService.toAddGoods(goods,model);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/addGoods")
    public String addGoods(Goods goods)
            throws IllegalStateException, IOException{
//        System.out.println("goods:"+goods.toString());
        String act="add";
        return goodsService.addGoods(goods,act);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/updateGoods")
    public String updateGood(Goods goods)
            throws IllegalStateException, IOException{
        String act="update";
        return goodsService.addGoods(goods,act);
    }


    @CrossOrigin //跨域访问
    @RequestMapping("/selectAllGoodsByPage")
    public String selectAllGoodsByPage(Model model,int currentPage,String act){
        return goodsService.selectAllGoodsByPage(model,currentPage,act);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/detail")
    public String detail(Model model,Integer id,String act){
        return goodsService.detail(model,id,act);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer id){
        return goodsService.delete(id);
    }



}
